package application.dispatcher;

import java.io.IOException;
import javax.servlet.ServletException;
import org.dsrg.soenea.domain.command.CommandException;
import domain.command.AddSingleUserCommand;



public class AddSingleUserDispatcher extends Dispatcher {

	public void execute() throws ServletException, IOException 
	{

		
			try
			{
				if(myRequest.getMethod() == "GET")
				{
					//check if admin
					if(!(Boolean)myHelper.getSessionAttribute("userisadmin") )
					{
						myHelper.setRequestAttribute("ERROR", "You are not a admin, so you cannot create a user.");
						throw new CommandException();
					}
					
					
					
					//execute command
					//new AddSingleUserCommand(myHelper, this.myRequest, this.myRequest.getMethod().equals("POST")).execute();
					
					//done executing, so lets move over to the view.
					myHelper.setRequestAttribute("viewFile", "AddUserTV");
					forward("/WEB-INF/JSP/template/template.jsp");
					
					
					
				} //if(this.myRequest.getMethod().equals("POST"))
				else if(myRequest.getMethod() == "POST")
				{
					//check if admin
					if(!(Boolean)myHelper.getSessionAttribute("userisadmin") )
					{
						myHelper.setRequestAttribute("ERROR", "You are not a admin, so you cannot create a user.");
						throw new CommandException();
					}
					
					
					//execute command
					//new AddSingleUserCommand(myHelper, this.myRequest, this.myRequest.getMethod().equals("POST")).execute();
					new AddSingleUserCommand(myHelper).execute();
					//done executing, so lets move over to the view.
					myHelper.setRequestAttribute("viewFile", "AddUserTV");
					forward("/WEB-INF/JSP/template/template.jsp");
					
					
				}
				else
				{
					myHelper.setRequestAttribute("ERROR", "Problem with adding a user. Please try again later.");
					throw new CommandException("Problem with adding a user. Please try again later.");
				}
				
			}
			catch (CommandException e)
			{
				forward("JSP/Errors.jsp");
			}
		
	}
}
